package com.phx.serialjni;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;


public class MeziView extends View {
    //定义相关变量,依次是妹子显示位置的X,Y坐标
    public float bitmapX;
    public float bitmapY;

    public int mleft;
    public int mtop;
    public int mwidth;
    public int mheight;

    public int mRes;
    public int type;
    public int ho;
    public int lv;
    public  int la;
    public int li;


    public MeziView(Context context) {
        super(context);
        //设置妹子的起始坐标
        bitmapX = 0;
        bitmapY = 200;
        mleft = 0;
        mtop = 0;
        mwidth = 50;
        mheight = 50;
        mRes = R.drawable.b2_off;
        type = 0;
        ho= 255;
        lv=255;
        la=255;
        li = 125;
    }

    //重写View类的onDraw()方法

    public Bitmap createImage(Rect mDestRect) {

        //由于直接new出来的view是不会走测量、布局、绘制的方法的，所以需要我们手动去调这些方法，不然生成的图片就是黑色的。

        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(mDestRect.width(), MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(mDestRect.height(), MeasureSpec.EXACTLY);

        measure(widthMeasureSpec, heightMeasureSpec);
        layout(0, 0, mDestRect.width(),mDestRect.height());
        Bitmap bitmap = Bitmap.createBitmap(mDestRect.width(), mDestRect.height(), Bitmap.Config.ARGB_8888);

        //  Bitmap bitmap = Bitmap.createBitmap(190, 110, Bitmap.Config.ARGB_8888);

        // Canvas canvas = new Canvas(bitmap);

        // canvas.drawColor(android.R.color.transparent);

        // canvas.drawColor(color);
        // Canvas canvas = new Canvas(bitmap);
        // draw(canvas);

        return bitmap;
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        getParent().requestDisallowInterceptTouchEvent(true);
//        return super.dispatchTouchEvent(ev);
//    }



    public void repaint()
    {
        //使当前页面无效，这样可以调用onDraw（）函数重新绘画当前界面
        this.invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //创建,并且实例化Paint的对象
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        //paint.setColorFilter(new LightingColorFilter(0x00ff00,0x000000));
        // paint.setColorFilter(new PorterDuffColorFilter(Color.rgb(192,92,192), PorterDuff.Mode.MULTIPLY));
        //根据图片生成位图对象
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), mRes);

        Rect mSrcRect, mDestRect;



        int mBitWidth = bitmap.getWidth();
        int mHalfWidth = bitmap.getWidth()/2;
        int mBitHeight = bitmap.getHeight();
        int mHalfHeight = bitmap.getHeight()/2;


        mSrcRect = new Rect(mleft, mtop, mBitWidth, mBitHeight);
        mDestRect = new Rect(mleft, mtop, mBitWidth, mBitHeight);

        // 计算左边位置
        int left = mHalfWidth - mBitWidth / 2;
        // 计算上边位置
        int top = mHalfHeight - mBitHeight / 2;
        mDestRect = new Rect(mleft, mtop, mleft + mwidth, mtop + mheight);

        if(type == 1)
        {
            // bitmap = Bitmap.createBitmap(mDestRect.width(), mDestRect.height(), Bitmap.Config.ARGB_8888);
            paint.setColor(Color.TRANSPARENT);
            canvas.drawRect(mDestRect,paint);

        }
        else {
           paint.setColorFilter(new PorterDuffColorFilter(Color.argb(li,ho,lv,la), PorterDuff.Mode.MULTIPLY));
            canvas.drawBitmap(bitmap, null, mDestRect, paint);
        }




        // Rect src=new Rect(mleft, mtop, bitmap.getWidth(), bitmap.getHeight());
        // Rect dst=new Rect(mleft, mtop, (mleft + mwidth), (mtop + mheight));

        // canvas.drawBitmap(bitmap, src, dst,paint);
        //判断图片是否回收,木有回收的话强制收回图片
        if(bitmap.isRecycled())
        {
            bitmap.recycle();
        }
    }
}